<?php

class Member_model extends Model {
	/**
	 * 根据邮箱获取用户信息
	 * @param unknown_type $email
	 */
	public function userInfoByEmail($email) {
		$dMember = new Member_data();
		$params['email'] = $email;
		return $dMember->userInfo($params);
	}
	/**
	 * 根据昵称获取用户信息
	 * @param unknown_type $nickname
	 */
	public function userInfoByNickname($nickname) {
		$dMember = new Member_data();
		$params['nickname'] = $nickname;
		return $dMember->userInfo($params);
	}
	/**
	 * 创建用户
	 * @param unknown_type $cols
	 */
	public function create($cols) {
		$dMember = new Member_data();
		$cols['password'] = md5($cols['password'].$cols['salt']);
		return $dMember->create($cols);
	}
	/**
	 * 保存邮箱验证码
	 * @param unknown_type $cols
	 */
	public function saveEmailVerify($cols) {
		$dMember = new Member_data();
		return $dMember->saveEmailVerify($cols);
	}
	
	/**
	 * 验证邮箱
	 * @param unknown_type $email
	 * @param unknown_type $code
	 */
	public function emailVerify($verify) {
		$dMember = new Member_data();
		$params['verify'] = $verify;
		return $dMember->emailVerify($params);
	}
	/**
	 * 用户登录
	 * @param unknown_type $username
	 * @param unknown_type $password
	 */
	public function login($email, $passwd) {
		$dMember = new Member_data();
		$params = array('email' => $email);
		$user = $dMember->userInfo($params);
		if(!$user) return -1;

		if(md5($passwd.$user['salt']) != $user['password']) return -2;
		if(!$user['is_active']) return -3;
		
		$result = $this->setOnline($user);
		if($result) {
			//写登录日志
			$this->loginLog($user);
		};
		return $this->correct($user);
	}
	/**
	 * 自动登录
	 * @param unknown_type $pt
	 * @param unknown_type $es
	 */
	public function autoLogin($pt, $es) {
		parse_str(htmlspecialchars_decode($pt), $cookie);
		if(!isset($cookie['uid']) || !isset($cookie['auto']) || $cookie['auto'] != 1) return false;
		
		//获取用户信息
		$dMember = new Member_data();
		$params['uid'] = $cookie['uid'];
		$user = $dMember->userInfo($params);
		if(!$user) return false;
		
		ksort($cookie);
		if($es != base64_encode(md5(implode(' ', $cookie).$user['password'].$user['salt']))) return false;

		//登陆成功		
		$result = $this->setOnline($user);
		if($result) {
			//写登录日志
			$params = array('uid' => $user['uid'], 'login_ip' => Func::clientIp(), 'login_time' => Func::time());
			$dMember->loginLog($params);
			return $this->correct($user);
		}else return false;
	}
	/**
	 * 获取当前登录用户
	 */
	public function currUser() {
		if(!(isset($_COOKIE['_PT']) && isset($_COOKIE['_ES']))) return false;
		if(empty($_COOKIE['_PT']) || empty($_COOKIE['_ES'])) return false;
		
		parse_str(htmlspecialchars_decode($_COOKIE['_PT']), $cookie);
		if(!isset($cookie['uid'])) return false;

		$dMember = new Member_data();
		if(isset($_COOKIE['token']) && $_COOKIE['token']) {
			//是否在线
			$params['uid'] = $cookie['uid'];
			$online = $dMember->onlineUser($params);
			if(!$online) return false;
			
			//获取用户信息
			$user = $dMember->userInfo($params);
			if(!$user) return false;
			
			ksort($cookie);
			$ES = base64_encode(md5(implode(' ', $cookie).$user['password'].$user['salt']));
			if(md5($ES.md5($cookie['rand'].$user['uid']).$user['salt']) != $_COOKIE['token']) return false;
	
			$result = $dMember->delayOnlineTime($user['uid'], Func::time(time()));
			if(!$result) return false;
			return $this->correct($user);
		} else {
			return $this->autoLogin($_COOKIE['_PT'], $_COOKIE['_ES']);
		}
	}
	/**
	 * 写登录日志
	 * @param unknown_type $user
	 */
	private function loginLog($user) {
		$dMember = new Member_data();
		$cols = array('uid' => $user['uid'], 'login_ip' => Func::clientIp(), 'login_time' => Func::time());
		$dMember->loginLog($cols);
	}
	/**
	 * 重组用户信息数组
	 * @param unknown_type $user
	 */
	private function correct($user) {
		if(!is_array($user)) return false;
		unset($user['password']);unset($user['salt']);
		return $user;
	}
	/**
	 * 设置用户在线
	 * @param unknown_type $user
	 */
	private function setOnline($user) {
		if(!$user['uid']) return false;

		$dMember = new Member_data();
		//删除所有过期
		$time = time();
		$overtime = $time-1800; //30分钟
		$params = array('last_active <' => Func::time($overtime));
		$dMember->delOffline($params);
		$cols = array('uid' => $user['uid'], 'last_active' => Func::time($time));
		$result = $dMember->setOneline($cols);
		if($result) $this->setcookie($user);
		return $result;
	}
	/**
	 * 设置用户cookie
	 * @param unknown_type $user
	 */
	private function setcookie($user) {
		$conf = Config::Instance();
		$cookie_domain = $conf->get('cookie_domain');
		//设置自动登录cookie
		$time = time();
		$ots = $time+2592000;
		$rand = Func::randstr(10);
		$cookie = array(
			'uid' => $user['uid'],
			'lts' => $time,
			'ots' => $ots, //一个月
			'rand'=> $rand,
			'un'  => $user['nickname'],
			'auto'=> '1',
			'ev'  => '1.0'
		);
		ksort($cookie);
		$ES = base64_encode(md5(implode(' ', $cookie).$user['password'].$user['salt']));
		$PT = http_build_query($cookie);
		$token = md5($ES.md5($cookie['rand'].$user['uid']).$user['salt']);
		setcookie('_ES', $ES, $ots, '/', $cookie_domain, false, true);//密文
		setcookie('_PT', $PT, $ots, '/', $cookie_domain, false, true);//明文
		setcookie('token', $token, 0, '/', $cookie_domain, false, true);//明文
	}
	/**
	 * 用户退出登录
	 */
	public function logout() {
		if(!isset($_COOKIE['_PT']) || empty($_COOKIE['_PT'])) return true;
		
		parse_str(htmlspecialchars_decode($_COOKIE['_PT']), $cookie);
		if(!isset($cookie['uid'])) return false;

		$dMember = new Member_data();
		$result = $dMember->logout($cookie['uid']);
		if($result) {
			$conf = Config::Instance();
			$cookie_domain = $conf->get('cookie_domain');
			setcookie('_ES', '', time()-1, '/', $cookie_domain, false, true);//清除密文
			setcookie('token', '', time()-1, '/', $cookie_domain, false, true);//清除会话明文
		}
		return $result;
	}
	/**
	 * 根据UID获取用户信息
	 * @param unknown_type $uid
	 */
	public function userInfoByUid($uid) {
		$dMember = new Member_data();
	
		$params = array('uid in' => $uid);
		$result =  $dMember->userInfos($params);
		if(!$result) return false;
		$return = array();
		foreach ($result as $user) {
			$return[$user['uid']] = $user;
		}
		return $return;
	}
}